import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { SysMessage } from '#/api/system/sysmessage';

import { useAccess } from '@vben/access';

import { z } from '#/adapter/form';
import { $t } from '#/locales';

const { hasAccessByCodes } = useAccess();

/**
 * 查询参数
 */
export function querySchema(): VbenFormSchema[] {
  return [
    {
      component: 'ApiSelect',
      componentProps: {
        buttonStyle: 'solid',
        clearable: true,
        options: [
          { label: $t('message.status.draft'), value: '0' },
          { label: $t('message.status.close'), value: '1' },
          { label: $t('message.status.send'), value: '2' },
        ],
        optionType: 'button',
      },
      defaultValue: null,
      fieldName: 'status',
      label: $t('common.status'),
    },
  ];
}

/**
 * 获取编辑表单的字段配置。如果没有使用多语言，可以直接export一个数组常量
 */
export function formSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'title',
      label: $t('message.title'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('message.title'), 2]))
        .max(50, $t('ui.formRules.maxLength', [$t('message.title'), 50])),
    },
    {
      component: 'Input',
      componentProps: {
        type: 'textarea',
      },
      fieldName: 'content',
      label: $t('message.content'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('message.content'), 2]))
        .max(1000, $t('ui.formRules.maxLength', [$t('message.content'), 1000])),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('message.status.draft'), value: '0' },
          { label: $t('message.status.close'), value: '1' },
          { label: $t('message.status.send'), value: '2' },
        ],
        optionType: 'button',
      },
      defaultValue: '0',
      fieldName: 'status',
      label: $t('common.status'),
    },
  ];
}

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function columnsSchema(
  onActionClick?: OnActionClickFn<SysMessage>,
): VxeTableGridOptions<SysMessage>['columns'] {
  return [
    { title: '序号', type: 'seq', width: 50 },
    {
      field: 'title',
      title: $t('message.title'),
    },
    {
      field: 'content',
      title: $t('message.content'),
    },
    {
      cellRender: {
        name: 'CellTag',
        options: [
          { label: $t('message.status.draft'), value: '0' },
          { label: $t('message.status.close'), value: '1' },
          { label: $t('message.status.send'), value: '2' },
        ],
      },
      field: 'status',
      title: $t('common.status'),
    },
    {
      field: 'createAt',
      title: $t('common.createTime'),
    },
    {
      cellRender: {
        attrs: {
          nameField: 'title',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: () => {
              return hasAccessByCodes(['sys_message:edit']);
            },
          },
          {
            code: 'delete',
            text: true,
            show: () => {
              return hasAccessByCodes(['sys_message:remove']);
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      align: 'center',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('page.action.action'),
    },
  ];
}
